# -*- coding: utf-8 -*-

import arcpy
import glob
import os

arcpy.CheckOutExtension('Spatial')

raw_root = u"C:\\Users\\91481\\Desktop\\毕业设计\\NDVI\\out\\"

# inws = "C:\\Users\\91481\\Desktop\\test\\2001"
#
# out = "C:\\Users\\91481\\Desktop\\test\\out\\"

out_root = u"C:\\Users\\91481\\Desktop\\毕业设计\\NDVI\\wuhan\\"

mask = "C:\\Users\\91481\\Desktop\\test\\wuhan\\wuhan.shp"


def extract(dir_name):
    raw = raw_root + dir_name + "\\"
    out = out_root + dir_name + "\\"
    if not os.path.exists(out):
        os.makedirs(out)
    rasters = glob.glob(os.path.join(raw, "*.tif"))

    for ras in rasters:
        name = out + ras.split("\\")[-1][:-4] + "-wuhan.tif"
        print name
        out_extract = arcpy.sa.ExtractByMask(ras, mask)
        out_extract.save(name)



# rasters = glob.glob(os.path.join(inws, "*.tif"))

# for ras in rasters:
#     name = out + ras.split("\\")[-1][:-4] + "-wuhan.tif"
#     print name
#     out_extract = arcpy.sa.ExtractByMask(ras, mask)
#     out_extract.save(name)


if __name__ == '__main__':
    for i in range(2000, 2021):
        extract(str(i))